home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Pascal
/
Book Demos in Pascal
/
StylDialog
/
StylDialog.p
< prev
next >
Wrap
Text File
|
1995-05-26
|
5KB
|
186 lines
program StylDialog;
(* StylDialog demo by Ingemar Ragnemalm 1995 *)
(* Based on a help system by Cary Torkelson *)
uses
{$IFC UNDEFINED THINK_PASCAL}
Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
OSUtils, ToolUtils, OSEvents, Resources, Memory,
{$ENDC}
QDOffScreen;
{ procedure ShowStyledTextDialog (textResourceID: Integer);}
var
theType: Integer;
itemHit: Integer;
itemhdl: Handle;
itembox: Rect;
theDialog: DialogPtr;
styledTextTEHandle: TEHandle;
textRect: Rect;
theScrollbar: ControlHandle;
numberLinesPerPage: Integer;
procedure ScrollTextToScrollValue;
var
oldScroll: Integer;
newScroll: Integer;
scrollbarValue: Integer;
begin
HLock(Handle(styledTextTEHandle));
oldScroll := styledTextTEHandle^^.viewRect.top - styledTextTEHandle^^.destRect.top;
scrollbarValue := GetCtlValue(theScrollbar);
if scrollbarValue = 0 then
newScroll := 0
else
newScroll := TEGetHeight(GetCtlValue(theScrollbar), 0, styledTextTEHandle);
TEScroll(0, oldScroll - newScroll, styledTextTEHandle);
HUnlock(Handle(styledTextTEHandle));
end; (*ScrollTextToScrollValue*)
procedure ScrollText (theControl: ControlHandle; thePart: Integer);
var
delta: Integer;
oldValue: Integer;
begin
case thePart of
inUpButton:
delta := -1;
inDownButton:
delta := 1;
inPageUp:
delta := -numberLinesPerPage;
inPageDown:
delta := numberLinesPerPage;
otherwise
end; {case}
if thePart <> 0 then
begin
oldValue := GetCtlValue(theControl);
SetCtlValue(theControl, oldValue + delta);
ScrollTextToScrollValue;
end;
end; (*ScrollText*)
function MyDialogFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: Integer): Boolean;
var
clickPoint: Point;
returnValue: Boolean;
thePart: Integer;
theControl: ControlHandle;
chcode: Integer;
begin
returnValue := false;
if theEvent.what = mouseDown then
begin
clickPoint := theEvent.where;
GlobalToLocal(clickPoint);
thePart := FindControl(clickPoint, theDialog, theControl);
if theControl = theScrollbar then
if thePart = inThumb then
begin
thePart := TrackControl(theScrollbar, clickPoint, nil);
ScrollTextToScrollValue;
end
else
thePart := TrackControl(theScrollbar, clickPoint, @ScrollText);
end
else if theEvent.what = keyDown then
begin
chcode := BAnd(theEvent.message, charCodeMask);
if (chcode = 13) or (chcode = 3) then
begin
itemHit := ok;
returnValue := true;
end;
end;
MyDialogFilter := returnValue;
end; (*MyDialogFilter*)
procedure ShowStyledTextDialog (textResourceID: Integer);
const
kHelpID = 128;
kHelpTextTEID = 2;
kScrollBarID = 4;
var
tempRect: Rect;
textResourceHandle: Handle;
stylResourceHandle: StScrpHandle;
maxScrollValue: Integer;
(*Get the dialog*)
begin
theDialog := GetNewDialog(kHelpID, nil, WindowPtr(-1));
SetPort(theDialog);
(*Get the user item defining where the text goes.*)
GetDItem(theDialog, kHelpTextTEID, theType, itemhdl, textRect);
(*Get the scrollbar handle*)
GetDItem(theDialog, kScrollBarID, theType, Handle(theScrollbar), tempRect);
(*Make a TextEdit record*)
styledTextTEHandle := TEStylNew(textRect, textRect);
(*Get the 'TEXT'/'styl' resources and insert them into the TextEdit record.*)
textResourceHandle := GetResource('TEXT', textResourceID);
stylResourceHandle := StScrpHandle(GetResource('styl', textResourceID));
HLock(textResourceHandle);
TEStylInsert(textResourceHandle^, SizeResource(textResourceHandle), stylResourceHandle, styledTextTEHandle);
HUnlock(textResourceHandle);
ReleaseResource(textResourceHandle);
ReleaseResource(Handle(stylResourceHandle));
(*Set scrollbar values depending on the text*)
numberLinesPerPage := (textRect.bottom - textRect.top) div (TEGetHeight(styledTextTEHandle^^.nLines, 0, styledTextTEHandle) div styledTextTEHandle^^.nLines);
maxScrollValue := styledTextTEHandle^^.nLines - numberLinesPerPage;
if maxScrollValue <= 0 then
HiliteControl(theScrollbar, 255)
else
SetCtlMax(theScrollbar, maxScrollValue);
(*Frame the text area and redraw it*)
tempRect := textRect;
InsetRect(tempRect, -1, -1);
FrameRect(tempRect);
TEUpdate(textRect, styledTextTEHandle);
InitCursor;
(*Run ModalDialog until the user clicks OK*)
itemHit := 0;
while itemHit <> ok do
ModalDialog(@MyDialogFilter, itemHit);
DisposeControl(theScrollbar);
DisposeDialog(theDialog);
TEDispose(styledTextTEHandle);
end; (*ShowStyledTextDialog*)
(* Standard inits *)
procedure InitToolbox;
begin
{$IFC UNDEFINED THINK_PASCAL}
InitGraf(@qd.thePort);
InitFonts;
FlushEvents(everyEvent, 0);
InitWindows;
InitMenus;
TEInit;
InitDialogs(nil);
{$ENDC}
InitCursor;
end; (*InitToolbox*)
(* Main program *)
begin
InitToolbox;
ShowStyledTextDialog(128);
end. (*main*)